//
//  BiTab.m
//  GottaWash
//
//  Created by bindu on 14-8-7.
//  Copyright (c) 2014年 QiHe. All rights reserved.
//

#import "BiTab.h"

// cross fade animation duration.
static const float kAnimationDuration = 0.15;

@interface BiTab ()

// Permits the cross fade animation between the two images, duration in seconds.
- (void)animateContentWithDuration:(CFTimeInterval)duration;

@end

@implementation BiTab
@synthesize showTipDot;

#pragma mark - Initialization

- (id)init
{
    self = [super init];
    if (self) {
        self.showTipDot = NO;
        self.contentMode = UIViewContentModeScaleAspectFit;
        self.backgroundColor = [UIColor clearColor];
        _titleIsHidden = NO;
    }
    return self;
}

#pragma mark - Touche handeling

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesEnded:touches withEvent:event];
    [self animateContentWithDuration:kAnimationDuration];
}

#pragma mark - Animation

- (void)animateContentWithDuration:(CFTimeInterval)duration
{
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"contents"];
    animation.duration = duration;
    [self.layer addAnimation:animation forKey:@"contents"];
    [self setNeedsDisplay];
}

#pragma mark - Drawing

- (void)drawRect:(CGRect)rect
{
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    
    // Setting the imageContainer's size.
    CGRect imageRect = CGRectZero;
    imageRect.size = _tabImage.size;
    imageRect.origin.x = (MAIN_SCREEN_WIDTH/4 - CGRectGetWidth(imageRect)) / 2;
    imageRect.origin.y = (32 - CGRectGetHeight(imageRect))/2;
    
    // Title label
    CGRect labelRect = CGRectMake(0, 32, MAIN_SCREEN_WIDTH/4, 12);
    
    if (self.showTipDot)
    {
        CGContextSaveGState(ctx);
        { //画圆和椭圆
            CGContextSetRGBStrokeColor(ctx, 255, 0, 0, 1);
            CGContextSetLineWidth(ctx, 4.0);
            CGContextAddEllipseInRect(ctx, CGRectMake(self.frame.size.width-10, 5, 4, 4));//在这个框中画圆
            CGContextStrokePath(ctx);
        }
        CGContextRestoreGState(ctx);
    }
    
    if (!self.selected)
    {
        // We draw the inner gradient
        CGContextSaveGState(ctx);
        {
            CGContextTranslateCTM(ctx, 0, 32);
            CGContextScaleCTM(ctx, 1.0, -1.0);
            CGContextClipToMask(ctx, imageRect, _tabImage.CGImage);
            
            CGContextDrawImage(ctx, imageRect, _tabImage.CGImage);
        }
        CGContextRestoreGState(ctx);
        
        CGContextSaveGState(ctx);
        {
            _textColor = UIColorFromRGB(0xb3b3b3);
            CGContextSetFillColorWithColor(ctx, _textColor.CGColor);
            [_tabTitle drawInRect:labelRect withFont:[UIFont systemFontOfSize:11] lineBreakMode:NSLineBreakByTruncatingMiddle  alignment:NSTextAlignmentCenter];
        }
        CGContextRestoreGState(ctx);
    }
    else if (self.selected)
    {
        // We draw the inner gradient
        CGContextSaveGState(ctx);
        {
            CGContextTranslateCTM(ctx, 0, 32);
            CGContextScaleCTM(ctx, 1.0, -1.0);
            CGContextClipToMask(ctx, imageRect, _tabSelectImage.CGImage);
            
            CGContextDrawImage(ctx, imageRect, _tabSelectImage.CGImage);
        }
        CGContextRestoreGState(ctx);
        
        CGContextSaveGState(ctx);
        {
            UIColor *textColor = UIColorFromRGB(0x009bf7);
            CGContextSetFillColorWithColor(ctx, textColor.CGColor);
            [_tabTitle drawInRect:labelRect withFont:[UIFont systemFontOfSize:11] lineBreakMode:NSLineBreakByTruncatingMiddle  alignment:NSTextAlignmentCenter];
        }
        CGContextRestoreGState(ctx);
    }
}

@end
